//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include <QAction>
#include "actiondispatcher.h"
#include "actionstate.h"
//
ActionState::ActionState(ActionDispatcher *disp,QAction *action,QObject *parent)
:
   QObject(parent),
   enabled(false),checked(false),
   dispatcher(0),
   action(0),
   active(false)
{
   if(disp!=0&&action!=0)
      attach(disp,action);
}
ActionState::~ActionState()
{
   if(is_attached())
      detach();
}
//
void ActionState::activate()
{
   if(is_attached())
      dispatcher->activate_handler(*this);
}
void ActionState::deactivate()
{
   if(is_attached())
      dispatcher->deactivate_handler(*this);
}
//
void ActionState::attach(ActionDispatcher *disp,QAction *action)
{
   if(dispatcher==disp&&this->action==action)
      return;
   detach();
   dispatcher=disp;
   this->action=action;
   disp->attach_handler(*this);
}
void ActionState::detach()
{
   if(dispatcher!=0)
      dispatcher->detach_handler(*this);
   dispatcher=0;
   action=0;
}
bool ActionState::is_attached() const
{
   return dispatcher!=0;
}
bool ActionState::is_active() const
{
   return active;
}
//
void ActionState::set_enabled(bool enabled)
{
   this->enabled=enabled;
   if(active)
      action->setEnabled(enabled);
}
void ActionState::set_checked(bool checked)
{
   this->checked=checked;
   if(active)
      action->setChecked(checked);
}
//
void ActionState::set_active()
{
   active=true;
   action->setEnabled(enabled);
   action->setChecked(checked);
}
void ActionState::set_inactive()
{
   active=false;
}
void ActionState::force_detach()
{
   dispatcher=0;
   action=0;
   active=false;
}
void ActionState::emit_triggered(bool checked)
{
   emit triggered(checked);
}
